﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary.CounterManager;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Plumber.Masters.ModuleData;

namespace Plumber.Masters.Forms
{
    public partial class MasterType : ApplicationLibrary.DBManager.DocumentForm
    {
        private EnumsManager<CodeMode> eCodeMode = null;
        private EnumsManager<MasterTypes> eMasterType = null;
        private EnumsManager<AccountType> eAccountType = null;
        private DUCodes duCodes = null;
        private ComboBoxManager cbbTaxCodeManager = null;
        private ComboBoxManager cbbRestraintManager = null;
        private RRTaxCode rRTaxCode = null;

        public MasterType()
            : base(Properties.Resources.FN_MasterType)
        {
            InitializeComponent();
            keyControl = txtCode;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarMasterTypesParam(txtCode.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerMasterType("mastertypeForm", new RadarMasterTypes(false), this);
            dbManager.AddMaster(this, PL_MasterType.Name, true);

            eCodeMode = new EnumsManager<CodeMode>(cbbCodeMode);
            eMasterType = new MasterTypeEnumManager(cbbMasterType);
            eAccountType = new EnumsManager<AccountType>(cbbAccountType);

            rRTaxCode = new RRTaxCode();
            cbbTaxCodeManager = new ComboBoxManager();
            cbbRestraintManager = new ComboBoxManager();
            cbbTaxCodeManager.AddValue(string.Empty, string.Empty);
            cbbRestraintManager.AddValue(string.Empty, string.Empty);

            rRTaxCode.Find();
            for (int t = 0; t < rRTaxCode.Count; t++)
            {
                cbbTaxCodeManager.AddValue(rRTaxCode.GetValue<string>(PL_TaxCode.Code, t), rRTaxCode.GetValue<string>(PL_TaxCode.Description, t));
                cbbRestraintManager.AddValue(rRTaxCode.GetValue<string>(PL_TaxCode.Code, t), rRTaxCode.GetValue<string>(PL_TaxCode.Description, t));
            }
            cbbTaxCodeManager.AttachTo(cbbTaxCode);
            cbbRestraintManager.AttachTo(cbbRestraint);

            duCodes = new DUCodes(false);
        }

        protected override void OnBindData()
        {
            BindControl(txtCode, PL_MasterType.Code);
            BindControl(txtDescription, PL_MasterType.Description);
            BindControl(cbbMasterType, PL_MasterType.MasterType, "SelectedValue", NullValue.NotSet);
            BindControl(cbbCodeMode, PL_MasterType.CodeMode, "SelectedValue", NullValue.NotSet);
            BindControl(cbbAccountType, PL_MasterType.AccountType, "SelectedValue", NullValue.NotSet);
            BindControl(ckbReceiveInv, PL_MasterType.CanReceiveInvoice, "Checked");
            BindControl(cbbTaxCode, PL_MasterType.TaxCode, "SelectedValue");
            BindControl(ckbRestraint, PL_MasterType.HasRestraint, "Checked");
            BindControl(ckbIsPrefer, PL_MasterType.IsPrefer, "Checked");
            BindControl(cbbRestraint, PL_MasterType.RestraintCode, "SelectedValue");
        }

        protected override void FocusOnNew()
        {
            txtCode.Focus();
        }

        protected override void OnDisableControlsForEdit()
        {
            txtCode.Enabled = false;

            if (eMasterType == null)
                return;

            if (eMasterType.GetValue() == MasterTypes.E_Administrator ||
                eMasterType.GetValue() == MasterTypes.E_PurchaseInvoice ||
                eMasterType.GetValue() == MasterTypes.E_SaleInvoice)
            {
                ckbReceiveInv.Checked = false;
                ckbReceiveInv.Enabled = false;
            }
            else
                ckbReceiveInv.Enabled = true;

            EnableDisableInvoiceData();
        }

        protected override void OnDisableControlsForNew()
        {
            EnableDisableInvoiceData();
        }

        protected override bool OnBeforeSave()
        {
            if (ckbReceiveInv.Checked && string.IsNullOrEmpty(cbbTaxCode.Text))
            {
                MyMessageBox(Properties.Resources.Msg_MissingTax,
                                Properties.Resources.Warning,
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (ckbRestraint.Checked && string.IsNullOrEmpty(cbbRestraint.Text))
            {
                MyMessageBox(Properties.Resources.Msg_MissingRestraint,
                                Properties.Resources.Warning,
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (eCodeMode.GetValue() != CodeMode.E_IsEmpty && !duCodes.Find(txtCode.Text))
            {
                CodesManager cm = new CodesManager();
                cm.AddCode(txtCode.Text, txtDescription.Text);
            }

            return base.OnBeforeSave();
        }

        protected override bool OnBeforeDelete()
        {
            if (!base.OnBeforeDelete())
                return false;

            if (duCodes.Find(txtCode.Text))
            {
                CodesManager cm = new CodesManager();
                cm.DeleteCode(txtCode.Text);
            }

            return true;
        }

        #endregion

        private void cbbMasterType_SelectedValueChanged(object sender, EventArgs e)
        {
            EnableDisableInvoiceData();
            OnDisableControlsForEdit();
        }

        private void ckbReceiveInv_CheckedChanged(object sender, EventArgs e)
        {
            EnableDisableInvoiceData();
        }

        private void ckbRestraint_CheckedChanged(object sender, EventArgs e)
        {
            EnableDisableInvoiceData();
        }

        private void EnableDisableInvoiceData()
        {
            cbbTaxCode.Enabled = ckbReceiveInv.Checked;
            ckbRestraint.Enabled = ckbReceiveInv.Checked;
            cbbRestraint.Enabled = ckbReceiveInv.Checked && ckbRestraint.Checked;
            if (!cbbRestraint.Enabled)
                cbbRestraint.Text = string.Empty;
        }
    }

    #region DbManager dbmanagerMasterType

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerMasterType : DBManager
    {
        public dbmanagerMasterType(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                if (e.Row[PL_MasterType.IsAccountingType.Name] == System.DBNull.Value)
                    e.Row[PL_MasterType.IsAccountingType.Name] = 0;
                if (e.Row[PL_MasterType.DocNoRequired.Name] == System.DBNull.Value)
                    e.Row[PL_MasterType.DocNoRequired.Name] = 0;
            }
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PL_MasterType();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PL_MasterType.Code, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", PL_MasterType.Code);
            sParam.Value = PL_MasterType.Code.DefaultValue;
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion

    internal class MasterTypeEnumManager : EnumsManager<MasterTypes>
    {
        public MasterTypeEnumManager(ComboBox cbbBox)
            : base(cbbBox)
        {
        }

        public override bool DisplayValue(MasterTypes t)
        {
            switch (t)
            {
                case MasterTypes.E_BankCash:
                case MasterTypes.E_PurchaseInvoice:
                case MasterTypes.E_SaleInvoice:
                case MasterTypes.E_Tax:
                    return false;
                default:
                    return true;
            }
        }
    }
}